package com.dididoctor.doctor.Activity.Order;

import android.content.Context;

import com.dididoctor.doctor.Activity.ConstantValue;
import com.dididoctor.doctor.Bean.Token;
import com.dididoctor.doctor.Http.MyHttpResponseHandler;
import com.dididoctor.doctor.MV.BasePresenter;
import com.dididoctor.doctor.MV.BusinessClient;
import com.dididoctor.doctor.MV.Response;
import com.dididoctor.doctor.Utils.Util;
import com.loopj.android.http.RequestParams;

import org.apache.http.Header;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @version 2.0
 * @类描述： 登陆页
 * @项目名称：ddgh
 * @类名称： loginActivity
 * @包名称： com.doudoukeji.service.Activity.login
 * @创建人：张强 boy
 * @创建时间：2016/3/7 16:40
 * @修改人：
 * @修改时间：2016/3/7 16:40
 * @修改备注： new parent class(用一句话描述修改了什么)
 */
public class OrderPresenter extends BasePresenter {
    private OrderView view;

    public OrderPresenter(Context context, OrderView view) {
        super(context, view);
        this.view = view;
    }

    public void getOrderList(String type, int pageIndex, final List<YXDoctorBean> ls) {
        RequestParams params = new RequestParams();
        params.put("userId", Token.getUserId());
        params.put("token", Token.getToken());
        params.put("type", type);
        params.put("pageSize", "10");
        params.put("pageIndex", pageIndex);
        BusinessClient.post(ConstantValue.orderlist, params, new MyHttpResponseHandler() {
            private List<Map<String, String>> maps = new ArrayList<Map<String, String>>();
            private List<OrderBean> orderBeans = new ArrayList<OrderBean>();
            @Override
            public void onMyResponse(Response response) {
                if (response.isSucceed()) {
                    maps = response.getListData("orderList");
                    for (Map<String, String> map : maps) {
                        OrderBean orderBean = new OrderBean();
//                       "accId": 14
                        orderBean.setOrderId(Util.toString(map.get("orderId")));
                        orderBean.setSex(Util.toString(map.get("sex")));
                        orderBean.setName(Util.toString(map.get("name")));
                        orderBean.setAge(Util.toString(map.get("age")));
                        orderBean.setOrderpackage(Util.toString(map.get("package")));
                        orderBean.setType(Util.toString(map.get("type")));
                        orderBean.setStatus(Util.toString(map.get("status")));
                        orderBean.setHeadPic(Util.toString(map.get("headPic")));
                        orderBean.setClientId(Util.toString(map.get("clientId")));
                        orderBean.setIsSign(Util.toString(map.get("isSign")));
                        orderBean.setPhone(Util.toString(map.get("phone")));
                        orderBean.setStartTime(Util.toString(map.get("startTime")));
                        orderBean.setAccId(Util.toString(map.get("accId")));

                        for (int x=0;x<ls.size();x++){
                            if (Util.toString(map.get("accId")).equals(ls.get(x).getContactId())){
                                orderBean.setChatstatus(ls.get(x).getChatstatus());
                            }
                        }
                        orderBeans.add(orderBean);
                    }
                    view.getdoctorsucced(orderBeans);
                } else {
                    view.showToastMessage(response.getMsg());
                }
            }

            @Override
            public void onFailure(int i, Header[] headers, byte[] bytes, Throwable throwable) {
                super.onFailure(i, headers, bytes, throwable);
                view.getdoctorfail();
            }
        });
    }

    public void getupdateStatus(String orderId, String status) {
        RequestParams params = new RequestParams();
        params.put("userId", Token.getUserId());
        params.put("token", Token.getToken());
        params.put("orderId", orderId);
        params.put("status", status);
        BusinessClient.post(ConstantValue.updatestatus, params, new MyHttpResponseHandler() {

            @Override
            public void onMyResponse(Response response) {
                if (response.isSucceed()) {
                    view.updatestatussucced();
                } else {
                    view.showToastMessage(response.getMsg());
                }
            }

            @Override
            public void onFailure(int i, Header[] headers, byte[] bytes, Throwable throwable) {
                super.onFailure(i, headers, bytes, throwable);
                view.updatesatusfail();
            }
        });
    }
}
